﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BingVis.Common
{
    public static class DateTimeExtensions
    {
        public static DateTime GetQuarterHour(this DateTime time)
        {
            return time.GetLastMinute(0, 15, 30, 45);
        }

        public static DateTime GetHalfHour(this DateTime time)
        {
            return time.GetLastMinute(0, 30);
        }

        public static DateTime GetHour(this DateTime time)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0, 0);
        }

        public static DateTime GetLastMinute(this DateTime time, params int[] mins)
        {
            if (mins.Contains(time.Minute))
                return new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0, 0);

            return GetLastMinute(time.AddMinutes(-1), mins);
        }

        public static DateTime WithTimeOfDay(this DateTime date, int hour, int minute, int second)
        {
            if (date == null)
                return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second);

            return new DateTime(date.Year, date.Month, date.Day, hour, minute, second);
        }

        public static DateTime WithTimeOfDay(this DateTime date, int hour)
        {       
            return date.WithTimeOfDay(hour, 0, 0);
        }

        public static DateTime WithTimeOfDay(this DateTime date, int hour, int minute)
        {
            return date.WithTimeOfDay(hour, minute, 0);
        }
    }
}
